package com.jkd.utils;


import cn.hutool.core.util.ReUtil;

public class JkConvertUtil {

    public static String decimalToAsc(String decimalStr) {
        StringBuilder result = new StringBuilder();
        String[] decimalArray = decimalStr.trim().split(" ");
        for (String str : decimalArray) {
            int i = Integer.parseInt(str);
            if (i < 128 && i > 0) {
                result.append((char) i);
            } else {
                result.append("/");
            }
        }
        return result.toString();
    }

    public static String bytesToHexString(byte[] src) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp;
        for (int i = 0; i < src.length; ++i) {
            temp = Integer.toHexString(src[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    public static String hexToAscii(String hexStr) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i += 2) {
            String str = hexStr.substring(i, i + 2);
            int parseInt = Integer.parseInt(str, 16);
            if (parseInt < 128 && parseInt > 0) {
                output.append((char) parseInt);
            } else {
                output.append("/");
            }
        }
        return output.toString();
    }

    //16进制字符串转10进制字符串
    public static String hexStrToDecStr(String hexStr) {
        StringBuilder output = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i += 2) {
            String str = hexStr.substring(i, i + 2);
            output.append(Integer.parseInt(str, 16)).append(" ");
        }
        return output.toString();
    }


    /**
     * 字节转10进制的字符/ 部分协议可以转为asc直接使用，部分协议需要转为10进制直接参与结果计算
     * 例子：
     * $6<0=00001?03
     * 36 54 60 48 61 48 48 48 48 49 63 48 51
     * 部分协议不需要转字符 直接将10进制的数字带入公式计算
     */
    public static String byteToStr(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte aByte : bytes) {
            stringBuilder.append(aByte + " ");
        }
        return stringBuilder.toString();
    }

    /**
     * 如果指令是#开头 并且是 232的指令模式 就需要转换指令 需要加上校验码等信息
     * 否则 直接将指令转换成16进制即可 这种方式是太钢 静力水准仪/测斜仪的指令方式
     * @param instructions
     * @return
     */
    public static String strToHex(String instructions) {
        if (instructions.startsWith("#") && ReUtil.isMatch("^\\d{8}[A]{1}$", instructions.replaceAll("#", ""))) {
            return checkCodeStr(instructions);
        } else {
            return instructions;
//            char[] chars = instructions.toCharArray();
//            StringBuilder sb = new StringBuilder();
//            for (char aChar : chars) {
//                String string = Integer.toHexString(aChar);
//                sb.append(string);
//            }
//            return sb.toString();
        }
    }

    public static String checkCodeStr(String sr) {
//        String sr = "#34210348A";
        char[] chars = sr.toCharArray();
        int a = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            String string = Integer.toHexString(chars[i]);
            sb.append(string);
            if (i == 0) continue;
            a += chars[i];
        }
        Integer in = Math.floorMod(a, 128) + 128;
        String string = Integer.toHexString(in);
        StringBuilder append = sb.append(string).append("21");
        return append.toString();
    }

    public static void main(String[] args) {
        System.out.println(strToHex("#34210348A"));
    }
}
